local Lang = require("common.Lang")
local ExcelMap = require("excel.Map")
local CopyConfig = ExcelMap.copy
local Obj = require("core.Obj")
local ItemDefine = require("bag.ItemDefine")
local BagLogic = require("bag.BagLogic")
local TaskDefine = require("task.Define")
local ObjHuman = require("core.ObjHuman")
local CopyL = require("copy.CopyL")

function handler(obj,list)
  
	if obj.obj_type~=Obj.TYPE_HUMAN then return true end
	
	for _,condition in ipairs(list) do
		local callback = _ENV[condition[1]]
		local retCode = callback(obj,condition[2],condition[3],condition[4]) 
		if retCode then
			return retCode
		end
	end
end

--***************条件****************

function level(obj,value)
	if Obj.getLv(obj) < value then 
		return string.format(Lang.COPY_ENTER_ERR_CONDITION_LV, value)
	end
end

function yinliang(obj,value)
	local currentValue = ObjHuman.getYinliang(obj)
	if currentValue < value then
		return string.format(Lang.COPY_ENTER_ERR_CONDITION_MONEY, value)
	end
end

function item(obj,itemID,count)	
  if itemID == ItemDefine.ITEM_ID_XUBAO_ITEM then
     count = CopyL.getXunbaoCostCnt(obj)
  end
	if BagLogic.getItemCount(obj,itemID) < count then
		local name = "【" .. ItemDefine.getValue(itemID,"name") .. "】"
		return string.format(Lang.COPY_ENTER_ERR_CONDITION_ITEM, name, count)
	end
end

function zhandouli(obj,value)
	if not obj or obj.obj_type ~= Obj.TYPE_HUMAN then
		return
	end	
	if obj.db.zhandouli < value then
		return string.format(Lang.COPY_ENTER_ERR_CONDITION_ZHANDOULI, value)
	end
end


function task(obj, taskID)
	if obj.db.taskRecord[taskID] == nil or obj.db.taskRecord[taskID].status ~= TaskDefine.TASK_ACCEPTED then 
		return Lang.COPY_ENTER_ERR_CONDITION_TASK
	end
end

function getItemCost(human, copyID)
	local copyConfig = CopyConfig[copyID]	
	for _,condition in ipairs(copyConfig.enterCondition) do
		if condition[1] == "item" then
			return condition[2], condition[3]
		end
	end
end

function tiaozhanval(obj, value)
  if not obj or obj.obj_type ~= Obj.TYPE_HUMAN then
    return
  end 
  if obj.db.remainTZ < value then
    return string.format(Lang.COPY_ENTER_ERR_CONDITION_TIAOZHANVAL, value)
  end
end
